﻿@namespace BlazorFluentUI.Lists
@inherits FluentUIComponentBase
@typeparam TItem
@typeparam TKey

<FocusZone Direction="FocusZoneDirection.Vertical"
              ClassName="ms-GroupedList">
    <List TItem="IGroupedListItem3<TItem>"
             @ref="listReference"
             ClassName=@($"ms-GroupedList{(string.IsNullOrEmpty(ClassName) ? "" : " " + ClassName)}")
             IsVirtualizing=@IsVirtualizing
             ItemsSource=@dataItems
             StartIndex=0>
        <ItemTemplate>
            @{
                #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
                if (context.Item is HeaderItem3<TItem, TKey>)
                {
                    var header = context.Item as HeaderItem3<TItem, TKey>;
                    bool isSelected = false;

                    if (Selection != null && header != null)
                    {
                        isSelected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                        if (!headerSubscriptions.ContainsKey(header))
                        {
                            var disposable = Selection.SelectionChanged.Subscribe(_ =>
                            {
                                Console.WriteLine("HeaderRangeCheck");
                                var selected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                                if (selected != isSelected)
                                {
                                    isSelected = selected;
                                    InvokeAsync(StateHasChanged);
                                }
                            });
                            headerSubscriptions.Add(header, disposable);
                        }
                    }
                    #pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
            
                    <GroupHeader IsOpen=@header!.IsOpen
                                 OnOpenChanged=@((isOpen)=>
                                                    {
                                                        if (header.IsOpen != isOpen)
                                                        {
                                                            header.IsOpen = isOpen;
                                                            InvokeAsync(StateHasChanged);
                                                        }
                                                    })
                                 Count=@header.Count
                                 GroupLevel=@header.Depth
                                 Name=@header.Name
                                 Compact=@Compact
                                 CurrentlySelected=@isSelected
                                 IsSelectionCheckVisible=@(Selection != null)
                                 OnClick=@(()=>OnHeaderClicked(context))
                                 OnToggle=@(()=>OnHeaderToggled(context)) />
                    if (header.IsOpen)
                    {
                        <GroupItem ItemsSource=@header.Items
                                   TItem=@TItem
                                   TKey=@TKey
                                   GetKey=@(GetKey!)
                                   IsVirtualizing=@IsVirtualizing
                                   Compact=@Compact
                                   ItemTemplate=@(this.ItemTemplate!)
                                   SelectionMode=@SelectionMode
                                   Selection=@Selection
                                   StartIndex=@header.GroupIndex
                                   OnHeaderClick=@((a)=>OnHeaderClicked(a))
                                   OnHeaderToggle=@((a)=>OnHeaderToggled(a)) />

                    }
                }
                else
                {
                    if (context.Item!.IsVisible)
                    {
                        @ItemTemplate!(context)
                    }
                }
            }
        </ItemTemplate>
    </List>

</FocusZone>